package com.nit.bike.service.basic.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.nit.bike.dto.basic.BikeDTO;
import com.nit.bike.dto.utils.PagingDTO;
import com.nit.bike.dto.utils.TableReqDTO;
import com.nit.bike.dto.utils.TableRspDTO;
import com.nit.bike.entity.basic.BikeEntity;
import com.nit.bike.mapper.basic.BikeMapper;
import com.nit.bike.service.basic.BikeService;
import ma.glasnost.orika.MapperFactory;
import ma.glasnost.orika.impl.DefaultMapperFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
public class BikeServiceImpl implements BikeService {
    @Resource
    private BikeMapper bikeMapper;

    @Override
    public TableRspDTO list4Table(TableReqDTO req) throws Exception {
        PageHelper.startPage(req.getPageNo(), req.getPageSize());
        List<BikeDTO> DTOs = bikeMapper.list4Table(
                req.parseQueryParam("bikeNumber"),
                req.parseQueryParam("bikeState"),
                req.parseQueryParam("stationId"),
                req.getSortField(),
                req.getSortOrder()
        );
        return new TableRspDTO(new PagingDTO(
                req.getPageNo(),
                req.getPageSize(),
                new PageInfo<>(DTOs).getTotal(),
                DTOs));
    }

    @Override
    public TableRspDTO listByStation(TableReqDTO req) throws Exception {
        PageHelper.startPage(req.getPageNo(), req.getPageSize());
        List<BikeDTO> DTOs = bikeMapper.listByStation(
                req.parseQueryParam("stationId"),
                req.parseQueryParam("bikeNumber"),
                req.parseQueryParam("bikeState"),
                req.getSortField(),
                req.getSortOrder()
        );
        return new TableRspDTO(new PagingDTO(
                req.getPageNo(),
                req.getPageSize(),
                new PageInfo<>(DTOs).getTotal(),
                DTOs));
    }

    @Override
    public BikeDTO query(Long bikeId) throws Exception {
        return bikeMapper.queryById(bikeId);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer add(BikeDTO bikeDTO) throws Exception {
        MapperFactory factory = new DefaultMapperFactory.Builder().build();
        BikeEntity entity = factory.getMapperFacade().map(bikeDTO, BikeEntity.class);
        Date now = new Date();
        entity.setGmtModify(now);
        entity.setGmtCreate(now);
        return bikeMapper.insertSelective(entity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer update(BikeDTO bikeDTO) throws Exception {
        MapperFactory factory = new DefaultMapperFactory.Builder().build();
        BikeEntity entity = factory.getMapperFacade().map(bikeDTO, BikeEntity.class);
        return bikeMapper.updateByPrimaryKeySelective(entity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer remove(String ids) throws Exception {
        String[] idArr = ids.split(",");
        for (String s : idArr) {
            bikeMapper.deleteByPrimaryKey(Long.parseLong(s));
        }
        return idArr.length;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer updateState(String ids, Integer state) throws Exception {
        String[] arrayBikeId = ids.split(",");
        for (String id : arrayBikeId) {
            bikeMapper.updateState(state, Long.parseLong(id));
        }
        return arrayBikeId.length;
    }

    @Override
    public void updateRideInfo(Long bikeId, Long rideId, Long stationId, Integer bikeState) throws Exception {
        bikeMapper.updateRideInfo(bikeId, rideId, stationId, bikeState);
    }
}
